package casa.depesos.app.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import androidx.annotation.Nullable;

import casa.depesos.app.com.R;
import casa.depesos.app.com.databinding.ActivityLoginBinding;
import casa.depesos.app.com.frag.CodeFragment;
import casa.depesos.app.com.frag.ForgetCodeFragment;
import casa.depesos.app.com.frag.ForgetPhoneFragment;
import casa.depesos.app.com.frag.NewPwdFragment;
import casa.depesos.app.com.frag.PhoneLoginFragment;
import casa.depesos.app.com.frag.PwdLoginFragment;
import casa.depesos.app.com.frag.SetPwdFragment;

public class LoginActivity extends BaseActivity {

    ActivityLoginBinding binding;
    int current = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        replaceFragment(R.id.frame, new PhoneLoginFragment());
    }

    @Override
    protected boolean needPadding() {
        return false;
    }

    public void changeWay(String phone) {
        if (current == 0) {
            current = 1;
            replaceFragment(R.id.frame, new PwdLoginFragment().setPhone(phone));
        } else {
            current = 0;
            replaceFragment(R.id.frame, new PhoneLoginFragment().setPhone(phone));
        }
    }

    public void changeWay(int i, String phone) {
        current = i;
        if (i == 0) {
            replaceFragment(R.id.frame, new PhoneLoginFragment());
        } else {
            replaceFragment(R.id.frame, new PwdLoginFragment());
        }
    }

    public void showCode(boolean hasRight, String phone, boolean isRegistered) {
        CodeFragment fragment = new CodeFragment();
        fragment.setParam(hasRight, current, phone, isRegistered);
        addFragment(R.id.frame, fragment);
    }

    public void toForget() {
        ForgetPhoneFragment fragment = new ForgetPhoneFragment();
        addFragment(R.id.frame, fragment);
    }

    public void toForgetCode(String phone, boolean hasRight) {
        ForgetCodeFragment fragment = new ForgetCodeFragment();
        fragment.setParam(phone, hasRight);
        addFragment(R.id.frame, fragment);
    }

    public void toNewPwd(String phone, String code, int type) {
        addFragment(R.id.frame, new NewPwdFragment().setParam(phone, code, type));
    }

    public void toSetPwd(String phone, String code) {
        SetPwdFragment fragment = new SetPwdFragment();
        fragment.setParam(phone, code, 0);
        addFragment(R.id.frame, fragment);
    }

    public void toHome() {
        Intent i = new Intent(this, MainActivity.class);
        startActivity(i);
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (fragments.size() > 1) {
                removeFragment(fragments.get(fragments.size() - 1));
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
